package com.zlm.linked;

/**
 * @author zlm
 * @date 2025/4/29
 */
public class Node<E> {

    public E value;
    public Node<E> next;

    public Node(E value, Node<E> next) {
        this.value = value;
        this.next = next;
    }

    public Node(E value) {
        this.value = value;
    }

    public void printNode() {
        Node<E> cur = this;
        while (cur != null) {
            System.out.print(cur.value+"\t");
            cur = cur.next;
        }
        System.out.println();
    }

    public void editNode(Node<Integer> head) {
        Node<Integer> node2 = new Node<>(2, null);
        //更改指针域
        head.next = node2;
        Node<Integer> node3 = new Node<>(3, null);
        node2.next = node3;
        Node<Integer> node4 = new Node<>(4, null);
        node3.next = node4;
        //指针赋值
        Node<Integer> tmp  = head;
        //指针移动
        tmp = tmp.next;
        //通过tmp指针变量更改节点的指针域：截断链表
        tmp.next = null;
        //通过tmp指针变量更改节点的指针域：增加节点
        tmp.next = new Node<>(100,null);
        head.next.next = null;
    }

    public void addNode(E value){
        if(this == null){
            return;
        }
        //创建一个新节点
        Node<E> newNode = new Node<>(value,null);
        Node<E> cur = this;
        while (cur.next != null){
            //移动指针
            cur = cur.next;
        }
        cur.next = newNode;
    }
}
